<?
	include("../main_inc.php");
	include("../../functions.inc.php");
	
	if($_GET['SFrom']) $SFrom=$_GET['SFrom'];
	elseif ($_POST['SFrom']) $SFrom=$_POST['SFrom'];
	if($_GET['SText']) $SText=$_GET['SText'];
	elseif ($_POST['STo']) $STo=$_POST['STo'];
	if($_GET['STo']) $STo=$_GET['STo'];
	elseif ($_POST['SText']) $SText=$_POST['SText'];
	if($_GET['SType']) $SType=$_GET['SType'];
	elseif ($_POST['SType']) $SType=$_POST['SType'];
	if($_GET['RType']) $RType=$_GET['RType'];
	elseif ($_POST['RType']) $RType=$_POST['RType'];
	else $RType="All";
	if($_GET['OType']) $OType=$_GET['OType'];
	elseif ($_POST['OType']) $OType=$_POST['OType'];
	else $OType="contactName";
	if($_GET['Page']) $Page=$_GET['Page'];
	elseif($_POST['Page']) $Page=$_POST['Page'];
	else $Page=1;
	
	$RecordsPerPage=10;
	
	if(!empty($SFrom)&&!empty($STo)){
		$SearchSQL.=" AND (EDateTime>='".SwapDates($SFrom)."' AND EDateTime<='".SwapDates($STo)." 23:59:59') ";
		
	}
	if($_POST['Domains'][0]!="All"){
		if($_POST['Domains']){
			if(is_array($_POST['Domains'])){
				$SearchSQL.=" AND visits.Domain IN (";
				$First=true;
				foreach($_POST['Domains'] as $val){
					if(!$First){
						$SearchSQL.=",";
						$Affs.=",";
					}
					$SearchSQL.=$val;
					$Affs.=$val;
					$First=false;
				}
				$SearchSQL.=") ";
			}
		}elseif(!empty($_GET['Affs'])){
			$_POST['Domains']=split(",",$_GET['Affs']);
			$SearchSQL.=" AND visit.Domain IN (";
			$First=true;
			foreach($_POST['Domains'] as $val){
				if(!$First){
					$SearchSQL.=",";
					$Affs.=",";
				}
				$SearchSQL.=$val;
				$Affs.=$val;
				$First=false;
			}
			$SearchSQL.=") ";
			$Affs=$_GET['Affs'];
		}
	}
	
	$SQL1="SELECT COUNT(*) FROM affiliate,affiliateEarn,affiliatePrograms,affiliateJoinedPrograms,visits,affiliateDomains WHERE affiliate.id=affiliateEarn.affiliateID AND affiliatePrograms.id=affiliateJoinedPrograms.affiliateProgramsID AND  affiliateJoinedPrograms.affiliateID=affiliate.id AND affiliate.id=$_SESSION[AffKey] AND visits.id=affiliateEarn.VisitsID AND visits.Domain=affiliateDomains.id  $SearchSQL";
	$rset=$r->rawQuery($SQL1);
	$rdata=mysql_fetch_array($rset);
	$rcount=$rdata[0];
	$MaxPages=ceil($rcount/$RecordsPerPage);
	if($Page>$MaxPages) $Page=$MaxPages;
	$StartRecord=($Page-1)*$RecordsPerPage;
	if($StartRecord<0) $StartRecord=0;
	/*
	$SQL="SELECT DATE_FORMAT(bookingDate,'%a %e %b %Y'),DATE_FORMAT(checkin,'%a %e %b %Y'),domain,propertyName,affiliateCommission FROM bookings,affiliateDomains WHERE bookings.affiliateDomainsID=affiliateDomains.id AND  bookings.affiliateID='$_SESSION[AffKey]' $BSQL";
	*/
	$SQL2="SELECT DATE_FORMAT(EdateTime,'%a %e %b %Y'),affiliatePrograms.Name,SaleAmount,CommissionAmount,EType FROM affiliate,affiliateEarn,affiliatePrograms,affiliateJoinedPrograms,visits,affiliateDomains WHERE affiliate.id=affiliateEarn.affiliateID AND affiliatePrograms.id=affiliateJoinedPrograms.affiliateProgramsID AND  affiliateJoinedPrograms.affiliateID=affiliate.id AND affiliate.id=$_SESSION[AffKey] AND visits.id=affiliateEarn.VisitsID AND visits.Domain=affiliateDomains.id $SearchSQL ORDER BY $OType LIMIT $StartRecord,$RecordsPerPage";
	$rset=$r->rawQuery($SQL2);
	
	//print $SQL2;
	$TargetUrl="affiliate.php";
	$NPPage="Affs=$Affs&RType=$RType&SFrom=$SFrom&STo=$STo&SType=$SType&OType=$OType&SText=".urlencode($SText);
	$RecTo=($StartRecord+$RecordsPerPage);
	if($RecTo>$rcount) $RecTo=$rcount;

?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Bubble Affiliate Program</title>

<link href="../../css/admin.css" rel="stylesheet" type="text/css">
<script src="../../jscript/menuArrayAffiliate.php" language="javascript"></script>
<script src="../../jscript/clock.js" language="javascript"></script>
<script language="JavaScript" src="<?=$rootdir?>jscript/calendar1.js"></script><!-- Date only with year scrolling -->
</head>

<body onLoad="funClock();">
<?php include("../header.php"); ?>
<table width="100%" border="0" cellspacing="25" cellpadding="0">
<tr>
<td valign="top" width="240"><div id="menu"><?php include("../menu.php"); ?></div></td>
<td valign="top" align="center"><div id="title">Sales Report </div>
  <div id="search">Enter search criteria.<br>
      <br>
      <form name="form1" method="post" action="sales.php">
        <table border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="60">Domain</td>
            <td><select name="Domains[]" size="5" multiple class="formFields" id="Domains[]">
                <option value='All' <?=($_POST['Domains'][0]=='All' ? "selected" : "");?>>All Domains</option>
                <?php
					$sq2=$r->rawQuery("SELECT id,domain FROM affiliateDomains WHERE affiliateID=$_SESSION[AffKey]");  
					while ($myrow = mysql_fetch_row($sq2)) {
						$tmp=(in_array($myrow[0],$_POST['Domains']) ? "selected" : "");
						echo"<option value='$myrow[0]' $tmp>$myrow[1]</option>";
					};
				?>
            </select></td>
            <td width="66" align="center">&nbsp;</td>
            <td>&nbsp;</td>
            </tr>
        </table>
        <br>
        <table width="271" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="60">From:</td>
            <td width="102"><input name="BFrom" type="text" class="formFields" id="BFrom" value="<?=($_POST['BFrom'] ? $_POST['BFrom'] :date("d-m-Y"));?>" size="10">
                <strong><a href="javascript:cal1.popup();"><img src="<?=$rootdir?>jscript/img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date" /></a></strong></td>
            <td width="23">To:</td>
            <td width="117"><input name="BTo" type="text" class="formFields" id="BTo" value="<?=($_POST['BTo'] ? $_POST['BTo'] :date("d-m-Y"));?>" size="10">
                <strong><a href="javascript:cal2.popup();"><img src="<?=$rootdir?>jscript/img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date" /></a></strong></td>
          </tr>
        </table>
        <p align="right">
          <input type="submit" name="Submit" value="Search" class="formButtons">
        </p>
      </form>
  </div>
  <div id="content">
    <table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#E9EEF5">
      <tr id="formHeader">
        <td width="49"><strong>Date</strong> </td>
        <td width="160"><strong>Program Name</strong></td>
        <td width="53" align="center"><strong>Amount</strong></td>
        <td width="81" align="center"><strong>Commission</strong></td>
        <td width="35" align="center"><strong>Type</strong></td>
      </tr>
      <?php
	$Count=0;
	if($rcount>0){
		while ($myrow = mysql_fetch_row($rset)) {
			$Total+=$myrow[2];
			$CTotal+=$myrow[3];
?>
      <tr bgcolor="<?=(($Count%2)==0 ? "#FFFFFF" : "#EBEBEB"); ?>">
        <td bgcolor="#FFFFFF"><?=$myrow[0];?></td>
        <td bgcolor="#FFFFFF"><?=$myrow[1];?></td>
        <td align="center" bgcolor="#FFFFFF">$
            <?=$myrow[2];?></td>
        <td align="center" bgcolor="#FFFFFF">$
            <?=$myrow[3];?>
          <a href="view-reservation.php?id=<?=$myrow[0];?>"></a></td>
        <td align="center" bgcolor="#FFFFFF"><?=$myrow[4];?></td>
      </tr>
      <?
		};
	};
?>
      <tr>
        <td bgcolor="#FFFFFF">&nbsp;</td>
        <td bgcolor="#FFFFFF"><strong>TOTAL</strong></td>
        <td align="center" bgcolor="#FFFFFF"><strong>$
          <?=number_format($Total,2);?>
        </strong></td>
        <td align="center" bgcolor="#FFFFFF"><strong>$
          <?=number_format($CTotal,2);?>
        </strong></td>
        <td align="center" bgcolor="#FFFFFF">&nbsp;</td>
      </tr>
    </table>
  </div>
  <div id="results2">
    <p><a href="#">EXPORT TO .CSV</a> </p>
  </div>
  <div id="content"></div>
  </td>
</tr>
</table>
<script language="JavaScript">
<!-- // create calendar object(s) just after form tag closed
	 // specify form element as the only parameter (document.forms['formname'].elements['inputname']);
	 // note: you can have as many calendar objects as you need for your application
	var cal1 = new calendar1(document.forms['form1'].elements['BFrom']);
	cal1.year_scroll = true;
	cal1.time_comp = false;
	var cal2 = new calendar1(document.forms['form1'].elements['BTo']);
	cal2.year_scroll = true;
	cal2.time_comp = false;
	
	

//-->
</script>
</body>
</html>
